home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / Updates / Librarys / zee_ini_library / Sources / C / Bin2H / Bin2H.c < prev    next >
C/C++ Source or Header  |  1999-08-05  |  4KB  |  177 lines

  1. /* Binary to C/C++ header file converter v1.0
  2. ** by Basty/Seasons (c) 1999. */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. int StrCmpNC(char *string1, char *string2);
  8. int ConvertBinary( void );
  9. int ConvertASCII( void );
  10.  
  11. #define ASCII 1
  12. #define BINARY 0
  13.  
  14. #define LINESIZE 16
  15.  
  16. #define CONV_OKAY 0
  17. #define CONV_ERROR_READ 1
  18. #define CONV_ERROR_WRITE 2
  19.  
  20. #define VERSION_MAJOR 1
  21. #define VERSION_MINOR 0
  22.  
  23. FILE *infile;
  24. FILE *outfile;
  25. unsigned long len;
  26.  
  27. int StrCmpNC(char *string1, char *string2)
  28. {
  29.   unsigned char c1,c2;
  30.  
  31.   do
  32.   {
  33.     c1 = (((c1 = *string1++) >= 'a' && c1 <= 'z') ? c1 - ' ' : c1);
  34.     c2 = (((c2 = *string2++) >= 'a' && c2 <= 'z') ? c2 - ' ' : c2);
  35.  
  36.     if (c1 != c2)
  37.       return(-1);
  38.   } while (c1 != 0 && c2 != 0);
  39.  
  40.   return(0);
  41. }
  42.  
  43. int ConvertBinary()
  44. {
  45.   unsigned char Buffer[LINESIZE];
  46.   unsigned int rlen;
  47.   unsigned long rpos = 0;
  48.   int i;
  49.  
  50.   while (rlen = fread(Buffer,1,LINESIZE,infile))
  51.   {
  52.     if (ferror(infile)) return CONV_ERROR_READ;
  53.     fputs("\n\t",outfile);
  54.     for (i = 0; i<rlen; i++)
  55.     {
  56.       rpos++;
  57.       fprintf(outfile,"0x%02x",*(Buffer+i));
  58.       if ((rpos != len) || i != rlen-1)
  59.         fputs(",",outfile);
  60.       if (i != rlen-1) fputs(" ",outfile);
  61.     }
  62.     if (ferror(outfile)) return CONV_ERROR_WRITE;
  63.   }
  64.   return(0);
  65. }
  66.  
  67. int ConvertASCII()
  68. {
  69.   unsigned char Buffer[LINESIZE],cbyte;
  70.   unsigned int rlen;
  71.   unsigned long rpos = 0;
  72.   int i;
  73.  
  74.   while (rlen = fread(Buffer,1,LINESIZE,infile))
  75.   {
  76.     if (ferror(infile)) return CONV_ERROR_READ;
  77.     fputs("\n\t",outfile);
  78.     for (i = 0; i<rlen; i++)
  79.     {
  80.       rpos++;
  81.       cbyte = Buffer[i];
  82.       if ((cbyte > 31) && (cbyte < 128))
  83.         fprintf(outfile,"'%c'",cbyte);
  84.       else
  85.         fprintf(outfile,"0x%02x",cbyte);
  86.       if ((rpos != len) || i != rlen-1)
  87.         fputs(",",outfile);
  88.       if (i != rlen-1) fputs(" ",outfile);
  89.     }
  90.     if (ferror(outfile)) return CONV_ERROR_WRITE;
  91.   }
  92.   return(0);
  93. }
  94.  
  95. int main(int argc,char **argv)
  96. {
  97.   char Mode = BINARY;
  98.   int conv = 0;
  99.  
  100.   printf("Bin2H v%d.%02d (c) 1999 by Basty/Seasons (%s, %s).\n\n", VERSION_MAJOR, VERSION_MINOR, __DATE__, __TIME__);
  101.  
  102.   if (argc != 3 && argc != 4) goto usage;
  103.   if (argc == 4)
  104.   {
  105.     if (StrCmpNC(argv[3],"ASCII")) goto usage;
  106.     Mode = ASCII;
  107.   }
  108.   if (!(infile = fopen(argv[1],"rb"))) goto openerr;
  109.  
  110.   fseek(infile,0,SEEK_END);
  111.   len = ftell(infile);
  112.   fseek(infile,0,SEEK_SET);
  113.  
  114.   if (ferror(infile)) goto openerr;
  115.  
  116.   if (!(outfile = fopen(argv[2],"w"))) goto createrr;
  117.  
  118.     fprintf(outfile,"/* Converted with binary to C-include.  (c) 1999 by Basty/Seasons. */\n\nunsigned char %s[%ld] =\n{",argv[1],len);
  119.  
  120.   switch (Mode)
  121.   {
  122.     case BINARY:
  123.     {
  124.       conv = ConvertBinary();
  125.  
  126.       break;
  127.     }
  128.     case ASCII:
  129.     {
  130.       conv = ConvertASCII();
  131.  
  132.       break;
  133.     }
  134.   }
  135.  
  136.   if (conv == CONV_OKAY) fputs("\n};\n",outfile);
  137.  
  138.   fclose(outfile);
  139.   fclose(infile);
  140.  
  141.   switch (conv)
  142.   {
  143.   case CONV_ERROR_READ:
  144.     goto readerr;
  145.   case CONV_ERROR_WRITE:
  146.     goto writerr;
  147.   }
  148.  
  149.   exit(0);
  150.  
  151.   usage:
  152.   printf("Invalid parameters !\n\nUsage is: %s <infile> <outfile> [ASCII]\n",argv[0]);
  153.  
  154.   exit(20);
  155.  
  156.   openerr:
  157.   printf("%s: Couldn't open %s.\nProgram terminated !\n",argv[0],argv[1]);
  158.  
  159.   exit(10);
  160.  
  161.   createrr:
  162.   fclose(infile);
  163.   printf("%s: Couldn't create %s.\nProgram terminated !\n",argv[0],argv[2]);
  164.  
  165.   exit(10);
  166.  
  167.   readerr:
  168.   printf("%s: Couldn't read in %s.\nProgram terminated !\n",argv[0],argv[1]);
  169.  
  170.   exit(10);
  171.  
  172.   writerr:
  173.   printf("%s: Couldn't write to %s.\nProgram terminated !\n",argv[0],argv[2]);
  174.  
  175.   exit(10);
  176. }
  177.